﻿Imports System.IO
Imports DevExpress.Web
Imports System.Data.SqlClient

Public Class AdminUpdateBook
    Inherits System.Web.UI.Page
    Dim sach As New Sach_BLL()
    Dim danhmuc As New DanhMuc_BLL()
    Dim p As New Sach_DTO()
    Dim d As New DanhMuc_DTO()
    Dim tendm, tenpl As String
    Dim pageControl As ASPxPageControl
    Dim cb As ASPxComboBox
    Dim ASPxTextBox_URL As ASPxTextBox
    Dim ASPxMemo_ChiTiet As ASPxMemo
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ASPxGridView1.DataBind()

    End Sub

    Public Sub loadSach()
        ASPxGridView1.DataSource = sach.Sach()
        ASPxGridView1.DataBind()
    End Sub

    Protected Sub ASPxGridView1_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating
        Dim hinh As ASPxUploadControl = TryCast(ASPxGridView1.FindEditRowCellTemplateControl(TryCast(ASPxGridView1.Columns("hinh"), GridViewDataColumn), "upload_hinh"), ASPxUploadControl)
        pageControl = TryCast(ASPxGridView1.FindEditFormTemplateControl("pageControl"), ASPxPageControl)
        ASPxMemo_ChiTiet = TryCast(pageControl.FindControl("ASPxMemo_ChiTiet"), ASPxMemo)
        Try
            p._idsach = e.Keys("id_sach")
            p._ten = e.NewValues("tensach")
            p._danhmuc = ASPxGridView1.GetRowValuesByKeyValue(e.Keys(ASPxGridView1.KeyFieldName), "tendm")
            p._ngaynhap = e.NewValues("ngaynhap")
            p._phanloai = cb.SelectedItem.Value.ToString()
            p._tacgia = Convert.ToString(e.NewValues("tacgia"))
            p._nxb = Convert.ToString(e.NewValues("nxb"))
            If hdf_hinh.Value.ToString() = String.Empty Then
                p._hinhanh = ASPxGridView1.GetRowValuesByKeyValue(e.Keys(ASPxGridView1.KeyFieldName), "hinh").ToString()
            Else
                p._hinhanh = "\img\book\" + hdf_hinh.Value.ToString()
                hdf_hinh.Value = String.Empty
            End If
            p._chitiet = ASPxMemo_ChiTiet.Text
            sach.CapNhapSach(p)
            e.Cancel = True
            ASPxGridView1.CancelEdit()

            loadSach()
        Catch
        End Try

    End Sub

    Protected Sub ASPxGridView1_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs) Handles ASPxGridView1.RowDeleting
        Try
            Dim imgPath As String = MapPath("~" + e.Values("hinh"))
            p._idsach = e.Values("id_sach")
            deleteImg(imgPath)
            sach.XoaSach(p)
            e.Cancel = True
            loadSach()
        Catch ex As SqlException
            ASPxGridView1.JSProperties("cp_showAlertMessage") = ex.Message
            e.Cancel = True
        End Try
    End Sub

    Public Sub deleteImg(ByVal path As String)
        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If
        Catch
        End Try

    End Sub


    Protected Sub ASPxGridView1_DataBinding(ByVal sender As Object, ByVal e As EventArgs) Handles ASPxGridView1.DataBinding
        ASPxGridView1.DataSource = sach.Sach()
    End Sub


    Protected Sub ASPxGridView1_CellEditorInitialize(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridViewEditorEventArgs) Handles ASPxGridView1.CellEditorInitialize
        Dim grid As ASPxGridView = sender
        tendm = grid.GetRowValues(e.VisibleIndex, "tendm")
        tenpl = grid.GetRowValues(e.VisibleIndex, "phanloai")
    End Sub

    Protected Sub cb_phanloai(ByVal sender As Object, ByVal e As System.EventArgs)
        d._tendm = tendm
        cb = TryCast(sender, ASPxComboBox)
        Dim container As GridViewDataItemTemplateContainer = TryCast(cb.NamingContainer, GridViewDataItemTemplateContainer)
        cb.DataSource = danhmuc.Sub_DanhMuc(d)
        cb.DataBind()
        cb.Text = tenpl
    End Sub


    Protected Sub upload_hinh_FileUploadComplete(ByVal sender As Object, ByVal e As DevExpress.Web.FileUploadCompleteEventArgs)
        Dim filename As String = e.UploadedFile.FileName
        e.UploadedFile.SaveAs(MapPath("~/img/book/") + filename)
    End Sub
End Class